package com.wnxy.jdk8.demo;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class App {
    public static void main(String[] args) throws Exception {
        //test01();  //6943
        //test2();   //6839
        test3();//175
    }
    private static void test01() {
        long start = System.currentTimeMillis();
        Long sum = 0L;
        for (Long i = 1L; i <=10_0000_0000; i++) {
            sum += i;
        }
        System.out.println("sum = " + sum);
        long end = System.currentTimeMillis();
        System.out.println("结果：" + sum + ",耗时：" + (end-start));
    }
    private static void test2() throws InterruptedException, ExecutionException {
        long start = System.currentTimeMillis();
        ForkJoinSumTask task = new ForkJoinSumTask(0L,10_0000_0000L);
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> forkJoinTask = forkJoinPool.submit(task);
        Long sum = forkJoinTask.get();
        long end = System.currentTimeMillis();
        System.out.println("结果：" + sum + ",耗时：" + (end-start));
    }
    private static void test3(){
        long start = System.currentTimeMillis();
        Long total = LongStream.rangeClosed(0,10_0000_0000).parallel().reduce(0,Long::sum);
        long end = System.currentTimeMillis();
        System.out.println("结果：" + total + ",耗时：" + (end-start));
    }
}